package cn.itlaobing.eshop.web.servlet;/**
 * Created by 1223 on 2017/11/15.
 */

import cn.itlaobing.eshop.dao.UsersDao;
import cn.itlaobing.eshop.entity.MsgToken;
import cn.itlaobing.eshop.entity.Users;
import cn.itlaobing.eshop.web.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

import static cn.itlaobing.eshop.web.servlet.MsgSendServlet.MSG_TOKEN_IN_SESSION;

/**
 * 类名：
 * 描述：
 * 作者：陈志凯
 * 时间：2017/11/15
 */
@WebServlet("/users/*")
public class UsersServlet extends BaseServlet{
    @RequestMapping("/change")
    public void change(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        toView(request,response,"/register");
    }
    @RequestMapping("/signin")
    public void signin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        toView(request,response,"/login");
    }
    @RequestMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        UsersDao usersDao=new UsersDao();
        String userAccount=request.getParameter("username");
        String userPassword=request.getParameter("password");
        try {
            Users users=usersDao.findByAccount(userAccount,userPassword);
            if(users==null){
                request.setAttribute("msg","用户名或者密码错误！");
                toView(request,response,"/login");
                return;
            }
            //登录成功后,将用户放入到session中，使用重定向
            request.getSession().setAttribute(MSG_TOKEN_IN_SESSION,users);
            response.sendRedirect(request.getContextPath()+"/goods/show");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/register")
    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UsersDao usersDao=new UsersDao();
        String userAccount=request.getParameter("username");
        String userPassword=request.getParameter("password");
        String userRqpwd=request.getParameter("rqpwd");
        String userMessage=request.getParameter("dxyzm");
        MsgToken msgToken= (MsgToken) request.getSession().getAttribute(MSG_TOKEN_IN_SESSION);
        String sendMessage=(String)request.getSession().getAttribute(msgToken.getMsgCode());

        if(!(userPassword.equals(userRqpwd))){
            request.setAttribute("msg","密码不一致");
            toView(request,response,"/register");
            return;
        }

        if(!(userMessage.equals(sendMessage))){
            request.setAttribute("msg","短信验证码有误！");
            toView(request,response,"/register");
            return;
        }

        try {
            int r=usersDao.addAccount(userAccount,userPassword);
            if(r<1){
                request.setAttribute("msg","注册失败");
                toView(request,response,"/register");

            }
            response.sendRedirect("/goods/show");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



}
